package com.axis2.common.util;


import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 字符串处理工具类
 * @author yemulin
 * @create 2012-8-20 下午5:26:13
 */
public class StrUtil {
	/**
	 * @Description: 将name1=value1&name2=value2 的请求转换成参数map
	 * @param 字符串
	 * @param 分隔符
	 * @return Map<String,String> 返回类型
	 * @throws UnsupportedEncodingException
	 */
	public static Map<String, String> getParameters(String strobj, String strmark) throws UnsupportedEncodingException {

		// 设置返回对象
		Map<String, String> resultMap = new HashMap<String, String>();

		String paraArray[] = strobj.split(strmark);

		for (String para : paraArray) {
			String paArray[] = para.split("=", 2);
			if (paArray != null && paArray.length >= 1) {
				String name = "";
				String value = "";
				if (paArray.length == 1) {
					name = HttpUtil.urlDecode(paArray[0]);
				} else {
					name = HttpUtil.urlDecode(paArray[0]);
					value = HttpUtil.urlDecode(paArray[1]);
				}
				resultMap.put(name, value);
			}
		}
		return resultMap;
	}
	/**
	 * @Description: 将name1=value1&name2=value2 的请求转换成参数map，原参数未经过encode
	 * @param 字符串
	 * @param 分隔符
	 * @return Map<String,String> 返回类型
	 * @throws UnsupportedEncodingException
	 */
	public static Map<String, String> getParametersNoDecode(String strobj, String strmark) throws UnsupportedEncodingException {

		// 设置返回对象
		Map<String, String> resultMap = new HashMap<String, String>();

		String paraArray[] = strobj.split(strmark);

		for (String para : paraArray) {
			String paArray[] = para.split("=", 2);
			if (paArray != null && paArray.length >= 1) {
				String name = "";
				String value = "";
				if (paArray.length == 1) {
					name = paArray[0];
				} else {
					name = paArray[0];
					value = paArray[1];
				}
				resultMap.put(name, value);
			}
		}
		return resultMap;
	}
	/**
	 * @Description: 字符串空值处理
	 * @param 原对象
	 * @return 处理结果
	 */
	public static String getVlaue(String str) {
		if (str == null) {
			str = "";
		}
		return str;
	}

	/**
	 * @Description: 从map中根据key取值,空值转换为""
	 * @param Key
	 * @param Map对象
	 * @return Key对象Value
	 */
	public static String get(String key, Map<String, String> map) {
		String result = "";
		if (map != null) {
			result = getVlaue(map.get(key));
		}
		return result;
	}

	/**
	 * 字符串转换为字节
	 * 
	 * @param 转换字符串
	 * @return 转换结果
	 */
	public static byte[] getStringToBytes(String obj) {
		int iLength = obj.length();
		if (iLength % 2 == 1) {
			return null;
		}
		byte[] ba = new byte[iLength / 2];
		for (int i = 0; i < iLength; i += 2) {
			ba[i / 2] = (byte) Integer.parseInt(obj.substring(i, i + 2), 16);
		}
		return ba;
	}

	/**
	 * 
	 * @Title: 获取匹配的值
	 */
	public static String getMatch(String request, String start, String end) {
		Matcher matcher = Pattern.compile("(" + start + ")(.+?)(" + end + ")").matcher(request);
		if (matcher.find()) {
			return matcher.group(2);
		}
		return null;
	}
	
	/**
	 * 
	 * @param reqXml
	 * @param start 卡号起始节点
	 * @param end 卡号结束节点
	 * @return 卡号加密范围前六后四
	 */
		public static String getLogReqStr(String reqXml, String start, String end) {
			String cardNo = StrUtil.getMatch(reqXml, start, end);
			if(StringUtils.isNotBlank(cardNo)) {
				String oldCardNoNode = start + cardNo + end;
				String newCardNoNode = null;
				if(cardNo.length() > 10) {
					newCardNoNode = start + cardNo.substring(0, 6) + "*********"
							+ cardNo.substring(cardNo.length() - 4, cardNo.length()) + end;
				} else {
					newCardNoNode = "****";
				}
				return reqXml.replace(oldCardNoNode, newCardNoNode);
			} else {
				return reqXml;
			}
		}

	/**
	 * 
	 * @param reqXml
	 * @param start
	 *            起始节点
	 * @param end
	 *            结束节点
	 * @return  全部为*号
	 */
		public static String getLogReqcardidStr(String reqXml, String start, String end) {
			String cardId = getMatch(reqXml, start, end);
			String oldCardNoNode = start + cardId + end;
			String newCardNoNode = start + "****" + end;
			return reqXml.replace(oldCardNoNode, newCardNoNode);
		}
		/**
		 * 
		 * @param reqXml
		 * @return 对报文敏感信息做掩盖处理
		 */
		public static String getbankcheckLogReqStr(String reqXml) {
			//证件号码
			reqXml = getLogReqcardidStr(reqXml,"<cardId>","</cardId>");
			//手机号码
			reqXml = getLogReqcardidStr(reqXml,"<mobilePhone>","</mobilePhone>");
			//cvv2
			reqXml = getLogReqcardidStr(reqXml,"<cvv2>","</cvv2>");
			//持卡人
			reqXml = getLogReqcardidStr(reqXml,"<cardHolder>","</cardHolder>");
			//账户号
			reqXml = getLogReqcardidStr(reqXml,"<AccNo>","</AccNo>");
			//合作方标示(存疑)
			reqXml = getLogReqcardidStr(reqXml,"<CorpAccNo>","</CorpAccNo>");
			//证件号码
			reqXml = getLogReqcardidStr(reqXml,"<IdCode>","</IdCode>");
			//卡号
			reqXml = getLogReqStr(reqXml,"<cardNo>","</cardNo>");
			//银行账户号
			reqXml = getLogReqStr(reqXml,"<bnkAccount>","</bnkAccount>");
			
			//对农行代扣通道senddate
			//卡号
			reqXml = getLogReqStr(reqXml,"<accNo>","</accNo>");
			//证件号码
			reqXml = getLogReqcardidStr(reqXml,"<idNo>","</idNo>");
			//持卡人名称
			reqXml = getLogReqcardidStr(reqXml,"<accNam>","</accNam>");
			//手机号码
			reqXml = getLogReqcardidStr(reqXml,"<mbNo>","</mbNo>");
			//付款人帐号
			reqXml = getLogReqStr(reqXml,"<oppAccNo>","</oppAccNo>");
			//付款人姓名
			reqXml = getLogReqcardidStr(reqXml,"<oppAccName>","</oppAccName>");
			//付款人手机
			reqXml = getLogReqcardidStr(reqXml,"<mobile>","</mobile>");
			//订单信息
			reqXml = getLogReqcardidStr(reqXml,"<Orderdetails>","</Orderdetails>");
			//对账单文件内容
			reqXml = getLogReqcardidStr(reqXml,"<FileContent>","</FileContent>");
			//有效期
			reqXml = getLogReqcardidStr(reqXml,"<expiredDate>","</expiredDate>");
			//手机验证码
			reqXml = getLogReqcardidStr(reqXml,"<chkCod>","</chkCod>");			
			//CAT类通道字段
			//卡有效期
			reqXml = getLogReqcardidStr(reqXml,"<expDate>","</expDate>");
			
			//出款类通道字段
			//付款账号
			reqXml = getLogReqcardidStr(reqXml,"<debitAccNo>","</debitAccNo>");
			//收款账户号
			reqXml = getLogReqcardidStr(reqXml,"<creditAccNo>","</creditAccNo>");
			//收款账户姓名
			reqXml = getLogReqcardidStr(reqXml,"<creditAccNameCn>","</creditAccNameCn>");
			//农行银企直联通道
			//收款方户名
			reqXml = getLogReqcardidStr(reqXml,"<CrAccName>","</CrAccName>");
			//借方帐号
			reqXml = getLogReqcardidStr(reqXml,"<DbAccNo>","</DbAccNo>");
			//贷方账号
			reqXml = getLogReqcardidStr(reqXml,"<CrAccNo>","</CrAccNo>");
			//工行银企直连通道
			reqXml = getLogReqcardidStr(reqXml,"<AccNo>","</AccNo>");
			return reqXml;
		}
		
		/**
		 * 
		 * @param reqXml xml报文
		 * @return 被掩盖的日志
		 */
		public static String hideFieldListForXml(String reqXml, String name) {
			String start = "<" + name + ">";
			String end = "</" + name + ">";
			
			String regex = start + "[^<]*" + end;
			String replacement = start + "****" + end;
			
			return reqXml.replaceAll(regex, replacement);
		}
		

		
		/**
		 * 
		 * @param reqXml xml报文
		 * @return 被掩盖的日志
		 */
		public static String getLogReqListForXml(String reqXml, List<String> names) {
			for(String name : names) {
				reqXml = hideFieldListForXml(reqXml, name);
			}
			
			return reqXml;
		}
}
